package com.maddox.maddoxppjoke.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.maddox.maddoxppjoke.R;
import com.maddox.maddoxppjoke.util.PixUtils;

public class ListPlayerView extends FrameLayout {


    private View bufferView;
    private PPImageView cover;
    private PPImageView blur;
    private ImageView playBtn;
    private String categray;
    private String videoUrl;

    public ListPlayerView(@NonNull Context context) {
        this(context, null);
    }

    public ListPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ListPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.layout_player_view, this, true);
        bufferView = findViewById(R.id.buffer_view);
        cover = findViewById(R.id.cover);
        blur = findViewById(R.id.blur_backgroud);
        playBtn = findViewById(R.id.cover);

    }

    /**
     * 视频会根据计算随着页面滑动显示出来时进行自动播放
     *
     * @param categray 区分页面
     * @param widthpx  视频宽度
     * @param heightpx 视频高度
     * @param coverUrl 覆盖图地址
     * @param videoUrl 视频地址
     */
    public void bindData(String categray, int widthpx, int heightpx, String coverUrl, String videoUrl) {
        this.categray = categray;
        this.videoUrl = videoUrl;
        //设置盖盖图
        cover.setImageUrl(coverUrl);
        //当宽度小于高度,竖屏播放时采用背景模糊
        if (widthpx < heightpx) {
            blur.setBlurImageUrl(coverUrl, 10);
            blur.setVisibility(VISIBLE);
        } else {
            blur.setVisibility(INVISIBLE);
        }

        setSize(widthpx, heightpx);

    }

    private void setSize(int widthpx, int heightpx) {
        int maxWidth = PixUtils.getScreenWith();
        int maxHeight = maxWidth;
        //布局需要设置的尺寸
        int layoutWidth = maxWidth;
        int layoutHeight = 0;
        //覆盖图的尺寸
        int coverWidth;
        int coverHeight;
        if (widthpx >= heightpx) {
            coverWidth = maxWidth;
            layoutHeight = coverHeight = (int) (heightpx / (widthpx * 1.0f / maxWidth));
        } else {
            coverWidth = (int) (widthpx / (heightpx * 1.0f / maxHeight));
            layoutHeight = coverHeight = maxHeight;
        }

        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        layoutParams.height = layoutHeight;
        layoutParams.width = layoutWidth;
        setLayoutParams(layoutParams);
        //模糊背景图的尺寸跟随视频尺寸
        ViewGroup.LayoutParams blurLayoutParams = blur.getLayoutParams();
        blurLayoutParams.width = layoutWidth;
        blurLayoutParams.height = layoutHeight;
        blur.setLayoutParams(blurLayoutParams);
        //覆盖图尺寸设置
        FrameLayout.LayoutParams coverParams = (LayoutParams) cover.getLayoutParams();
        coverParams.width = coverWidth;
        coverParams.height = coverHeight;
        coverParams.gravity = Gravity.CENTER;
        cover.setLayoutParams(coverParams);
        //播放按钮尺寸设置
        FrameLayout.LayoutParams playBtnParams = (LayoutParams) playBtn.getLayoutParams();
        playBtnParams.gravity = Gravity.CENTER;
        playBtn.setLayoutParams(playBtnParams);

    }
}
